﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class CallReasonDataAccess : BaseDataAccess<Business.Entities.CallReason>
    {
        public override GMAC.ReportingSystem.Business.Entities.CallReason Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procCallReasonByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public CallReasonCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procCallReasonSelect", null);
            return ReadTable(dt) as CallReasonCollection;
        }

        //-----------------------------------------------------------------------------------
        /// <summary>
        /// Ricardo Recaredo (25/03/2011)
        /// </summary>
        /// <param name="accountTypeId">Product Id</param>
        /// <returns>CalllReasons Collection filtered by Product</returns>
        //-----------------------------------------------------------------------------------

        public CallReasonCollection GetListByProduct(int accountTypeId)
        {
            ArrayList parameters = new ArrayList() { accountTypeId };

            DataTable dt = DataAccessComponent.ExecuteSelect("procListCallReasonsByProduct", parameters);

            return ReadTable(dt) as CallReasonCollection;
        }

        //-----------------------------------------------------------------------------------

        protected override GMAC.ReportingSystem.Business.Entities.CallReason ReadRow(System.Data.DataRow row)
        {
            CallReason CallReason = null;
            if (row != null)
            {
                try
                {
                    CallReason = new CallReason();
                    CallReason.ID = (int)row["ID"];
                    CallReason.Description = row["Description"].ToString();
                    CallReason.AccounTypeID = int.Parse(row["AccounTypeID"].ToString());
                    CallReason.Status = bool.Parse(row["Status"].ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return CallReason;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.CallReason> ReadTable(System.Data.DataTable table)
        {
            try
            {
                CallReasonCollection CallReasons = new CallReasonCollection();
                CallReason CallReason;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CallReason = ReadRow(row);
                        if (CallReason != null)
                        {
                            CallReasons.Add(CallReason);
                        }
                    }
                }
                return CallReasons;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public CallReasonCollection Search(string Description, int? ProductType, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { Description, ProductType, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procCallReasonByFilterSelect", parameters);
                CallReasonCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as CallReasonCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}


